package com.supermap.business.disaster;

import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.disaster.DisasterDIPService;
import com.supermap.dubbo.api.disaster.DisasterService;
import com.supermap.dubbo.api.disaster.entity.Disaster;
import com.supermap.dubbo.api.disaster.entity.DisasterDIP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @Author:langhongshuai
 * @Description:病虫害接口层
 * @Date:18/1/29 下午2:37
 */
@Controller
@RequestMapping("DisasterDIPController")
public class DisasterDIPController {
    /**
     * 使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(DisasterDIPController.class);

    @Reference
    private DisasterDIPService disasterDIPService;

    /**
     * 通过用户id和区域code获得用户的病虫害信息
     */
    @RequestMapping("/getDisasterDIPByUIdArea")
    @ResponseBody
    public JSONUtil getDisasterDIPByUIdArea(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String areaCode = request.getParameter("areaCode");
        return disasterDIPService.getDisasterDIPByUIdArea(userId, areaCode);
    }

    /**
     * 添加病虫害信息
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/addDisasterDIP", method = RequestMethod.POST)
    @ResponseBody
    public JSONUtil addDisasterDIP(@RequestBody HashMap<String, String> data) {
        String userId = data.get("userId");
        String farmlandId = data.get("farmlandId");
        String type = data.get("type");
        String breed = data.get("breed");
        String growthPeriod = data.get("growthPeriod");
        String longitude = data.get("longitude");
        String latitude = data.get("latitude");
        String areaCode = data.get("areaCode");
        String descript = data.get("descript");
        String imgUrl = data.get("imgUrl");
        String disasterType = data.get("disasterType");
        String recordtime = data.get("recordtime");
        String development = data.get("development");

        DisasterDIP disasterdip = new DisasterDIP();

        disasterdip.setUserId(userId);
        disasterdip.setFarmlandid(farmlandId);
        disasterdip.setType(type);
        disasterdip.setBreed(breed);
        disasterdip.setGrowthperiod(growthPeriod);
        disasterdip.setLatitude(latitude);
        disasterdip.setLongitude(longitude);
        disasterdip.setAreacode(areaCode);
        disasterdip.setDescription(descript);
        disasterdip.setImgUrl(imgUrl);
        disasterdip.setDisastertype(disasterType);
        disasterdip.setRecordtime(recordtime);
        disasterdip.setDevelopment(development);
        return disasterDIPService.addDisasterDIP(disasterdip);
    }

    /**
     * 获得所有的病虫害信息
     *
     * @return
     */
    @RequestMapping("/getAllDisasterDIP")
    @ResponseBody
    public JSONUtil getAllDisasterDIP() {
        return disasterDIPService.getAllDisasterDIP();
    }

    /**
     * 删除病虫害信息
     * @param request
     * @return
     */
    @RequestMapping("/delDisasterDIP")
    @ResponseBody
    public JSONUtil delDisasterDIP(HttpServletRequest request){
        String disasterId = request.getParameter("disasterId");
        return disasterDIPService.delDisasterDIP(Integer.parseInt(disasterId));
    }

    /**
     * 根据区域编码得到病虫害信息
     * @param request
     * @return
     */
    @RequestMapping("/getDisasterDIPByareacode")
    @ResponseBody
    public JSONUtil getDisasterDIPByareacode(HttpServletRequest request){
        String areacode = request.getParameter("areacode");
        return disasterDIPService.getDisasterDIPByareacode(areacode);
    }

    /**
     * 获得病虫害信息
     */
    @RequestMapping("/getDisasterDIP")
    @ResponseBody
    public JSONUtil getDisasterDIP(HttpServletRequest request){
        String type = request.getParameter("cropid");
        String areaCode = request.getParameter("areaCode");
        String disasterType = request.getParameter("disasterType");
        String dateTime = request.getParameter("dateTime");
        return disasterDIPService.getDisasterDIP(type,areaCode,disasterType,dateTime);
    }
}
